<?php use_helper('jQuery')?>
<script type="text/javascript">
    $(function() {
        $("input:submit").button();
    });
    $( ".ac_input" ).autocomplete({
        source: "<?php echo url_for('nominaciones_getcandidates', $form->getObject()->getCategory())?>",
        minLength: 1,
        focus: function( event, ui ) {
            $( "#nomination_autocomplete_input" ).val( ui.item.label );
            return false;
        },
        select: function( event, ui ) {
            $('#nomination_autocomplete_input').val(ui.item.label);
            $('#nomination_candidate_id').val(ui.item.value);
            return false;
        },
        open: function() {
            $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
        },
        close: function() {
            $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    }).data( "autocomplete" )._renderItem = function( ul, item ) {
        var desc = item.desc.length <= 120 ? item.desc : item.desc.substr(0, 120)+"...";
        var height_adjust = desc.length > 70 ? "style=\"height:60px;\"" : "";
        return $( "<li class='ac_option'></li>" )
            .data( "item.autocomplete", item )
            .append( "<a "+height_adjust+"><ul><li><span class='ac_option_image' style='background: url(/uploads/"+item.icon+") no-repeat'></li></span><li><span class='ac_option_info'>" + item.label + "<br><em class='small'>" + desc + "</em></span></li></ul></a>")
            .appendTo( ul );
    };
</script>

<?php echo jq_form_remote_tag(
        array(
            'update' => 'main-content',
            'url' => $form->getObject()->isNew() ?
                    url_for('nominaciones_create', $form->getObject()->getCategory())
                            :
                    url_for('nominaciones_update', $form->getObject()),
            'before' => 'showLoader();',
            'complete' => 'hideLoader();',
            'script' => true
        ), array(
            'id' => 'nomination-form',
            'class' => ''
        )); ?>
<?php if (!$form->getObject()->isNew()): ?>
    <input type="hidden" name="sf_method" value="put" />
<?php endif; ?>
    <div class="span-17 last">
        <?php echo $form ?>
        <div class="prepend-top prepend-7 span-10 last">
            <input type="submit" value="Nominar" />
        </div>
        <div id="notfound" class="prepend-top span-17 last">
            <p>
                Si no encuentras a tu candidato en la lista haz click
                <?php echo jq_link_to_remote('aquí', 
                    array(
                        'url' => url_for('nominaciones_new_candidate',$form->getObject()->getCategory()),
                        'update' => 'main-content',
                        'before' => 'showLoader();',
                        'complete' => 'hideLoader();',
                        'script' => true
                    ), array(
                        'class' => ''
                    ));
                ?>
            </p>
        </div>
        <div id="prev-nominee" class="span-17 last">
            <?php if (!$form->getObject()->isNew()): ?>
                <h3>Has nominado en esta categoría a: <b><?php echo $form->getObject()->getCandidate() ?></b></h3>
            <?php endif; ?>
            <p class="prepend-2 append-2">
                Recuerda que sólo puedes nominar a un candidato en cada categoría,
                pero puedes cambiar a tu nominado cuantas veces quieras
            </p>
        </div>
        <div id="backtocategories" class="span-17 last">
            <?php echo jq_link_to_remote('Volver a Categorias', 
                array(
                    'url' => url_for('nominaciones_categories'),
                    'update' => 'main-content',
                    'before' => 'showLoader();',
                    'complete' => 'hideLoader();',
                    'script' => true
                ), array(
                    'class' => 'backtocategories'
                ));
            ?>
        </div>
    </div>
</form>
